<template>
    <div class="mainDiv order">
        <div class="rowHead">
            <el-button type="primary" @click="addneworder" icon="h-kaidan" v-qx="quanxian.add">开单</el-button>
            <el-button type="primary" @click="reupdate" class="fr ml" icon="h-shuaxin1" style="float:right">刷新列表</el-button>
        </div>
        <el-row :gutter="20" class="rowHead">
            <el-col :span="16" id="el-col-16">
                <el-tag type="success">筛选</el-tag>
                <el-date-picker v-model="filter.times" type="daterange" align="right" placeholder="请选择日期范围" :picker-options="pickerOptions2" @change="reupdate">
                </el-date-picker>
                <el-select v-model="filter.status" @change="reupdate" class="ml w200" v-qx="quanxian.status">
                    <el-option v-for="(o,index) in zhuanttai" :label="o.txt" :value="o.val" :key="index">
                    </el-option>
                </el-select>
                <el-button type="primary" @click="outXlsx" class="fr ml">导出数据</el-button>
            </el-col>
            <el-col :span="8">
                <el-input placeholder="搜索单号、姓名、手机号、会员卡号" v-model="filter.search" @keyup.enter.native="searchStr">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="searchStr"></i>
                </el-input>
            </el-col>
        </el-row>
        <el-table :data="list" :stripe="true" height="100%">
            <!-- <el-table-column prop="id" label="ID" width="60"></el-table-column> -->
            <el-table-column prop="order_sn" label="单号" width="180"></el-table-column>
            <el-table-column prop="gname" label="姓名">
                <template slot-scope="scope">
                    <span @click="openGuestPage(scope.row.guestid)" style="cursor:pointer;color:#f50">{{scope.row.gname}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="shopname" label="所属分店" :show-overflow-tooltip="true">
            </el-table-column>
            <el-table-column prop="gphone" label="手机号" width="110">
                <template slot-scope="scope">{{scope.row.gphone?scope.row.gphone:'无'}}</template>
            </el-table-column>
            <el-table-column prop="vipcard" label="会员卡号" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="recemoney" label="现金支付">
                <template slot-scope="scope">{{scope.row.recemoney+'元'}}</template>
            </el-table-column>
            <el-table-column prop="cardreduce" label="卡付">
                <template slot-scope="scope">{{scope.row.cardreduce+'元'}}</template>
            </el-table-column>
            <el-table-column prop="usebalance" label="余额支付">
                <template slot-scope="scope">{{scope.row.usebalance+'元'}}</template>
            </el-table-column>
            <el-table-column prop="usegiftbalance" label="赠送余额支付">
                <template slot-scope="scope">{{scope.row.usegiftbalance+'元'}}</template>
            </el-table-column>
            <el-table-column prop="addtime" label="开单日期" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{scope.row.addtime|hourTime}}
                </template>
            </el-table-column>
            <el-table-column prop="operatorname" label="操作员"></el-table-column>
            <el-table-column prop="auditorname" label="审核员">
                <template slot-scope="scope">{{scope.row.auditorname?scope.row.auditorname:'暂无'}}</template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="80">
                <template slot-scope="scope">
                    <Tags :typeValue="scope.row.status" typeName="orderStatus" :tagStyle="{color: '#fff'}"></Tags>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="150" fixed="right">
                <template slot-scope="scope">
                    <el-button-group v-if="scope.row.status==0">
                        <el-button icon="el-icon-h-chakan" type="warning" size="mini" @click="tongyong(scope.row)" v-qx="quanxian.look"></el-button>
                        <!-- <el-tooltip content="结账" placement="top" :open-delay="800" v-qx="quanxian.jiezhang">
                  <el-button icon="h-jiezhang" type="success" size="small" @click="jiezhang(scope.row.id)"></el-button>
                </el-tooltip> -->
                        <el-button icon="el-icon-delete" type="danger" size="mini" @click="shanchu(scope.row.id)" v-qx="quanxian.del"></el-button>
                    </el-button-group>
                    <el-button-group v-else>
                        <el-button icon="el-icon-h-chakan" type="warning" size="mini" @click="tongyong(scope.row)" v-qx="quanxian.look"></el-button>
                        <el-button type="primary" icon="el-icon-h-dayin" size="mini" @click="dayin(scope.row.id)" v-qx="quanxian.print"></el-button>
                        <el-button type="danger" icon="el-icon-h-qiyong" size="mini" @click="fanshenhe(scope.row.id)" v-qx="quanxian.fan"></el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top:5px">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-sizes="[15, 30, 50, 100]" :current-size="filter.currentSize" :page-size="filter.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '@/common/common.js'
import P from '@/service/shop/popup.js'
import Guest from '@/service/shop/guest.js'
import Tags from '@/components/orderTags.vue'
import Order from '@/service/shop/order.js'
export default {
  name: 'orderList',
  data () {
    return {
      list: [],
      filter: {
        search: '',
        pageSize: 15,
        currentSize: 1,
        status: 0,
        times: [new Date(), new Date()]
      },
      total: 0,
      zhuanttai: [{
        txt: '请选择单据状态',
        val: 0
      }, {
        txt: '未结账',
        val: 1
      }, {
        txt: '已结账未分配',
        val: 2
      }, {
        txt: '已结账已分配',
        val: 3
      }],
      quanxian: {
        add: 'order_add',
        status: 'order_status',
        del: 'order_del',
        look: 'order_look',
        print: 'order_print',
        fan: 'order_fan'
      },
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const td = new Date()
            td.setHours(0)
            td.setMinutes(0)
            td.setSeconds(0)
            td.setMilliseconds(0)
            const start = new Date()
            start.setTime(td.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, td])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const td = new Date()
            td.setHours(0)
            td.setMinutes(0)
            td.setSeconds(0)
            td.setMilliseconds(0)
            const start = new Date()
            start.setTime(td.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, td])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const td = new Date()
            td.setHours(0)
            td.setMinutes(0)
            td.setSeconds(0)
            td.setMilliseconds(0)
            const start = new Date()
            start.setTime(td.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, td])
          }
        }]
      }
    }
  },
  methods: {
    searchStr: function () {
      this.reupdate()
    },
    addneworder: async function () {
      await P.openOrderPopup()
      this.reupdate()
    },
    sizeChange: function (val) {
      this.filter.pageSize = val
      this.reupdate()
    },
    currentChange: function (val) {
      this.filter.currentSize = val
      this.reupdate()
    },
    async shanchu (id) {
      C.confirm('确定要删除吗？', async () => {
        let data = await C.ajax('/shop/order/deleteorder', { 'id': id }, 'post')
        if (data) {
          C.success(data.data.info)
          this.reupdate()
        }
      })
    },
    dayin: function (id) {
      C.openBrowserWindow('/printOpenOrder', { id: id }, { width: 245, height: 600, useSpa: false, spaModel: 'print' })
    },
    async reupdate () {
      let data = await Order.getOrderList({ filter: JSON.stringify(this.filter) })
      if (data) {
        this.total = data.count
        this.list = data.data
      }
    },
    async fanshenhe (id) {
      C.confirm('确定要反结账吗？', async () => {
        let data = await C.ajax('/shop/order/fanjiezhang', { 'id': id }, 'post')
        if (data) {
          C.success(data.data.info)
          this.reupdate()
        }
      })
    },
    tongyong: async function (row) {
      await P.openOrderPopup({ id: row.id, status: row.status })
      this.reupdate()
    },
    async outXlsx () {
      let header = {
        order_sn: '单号',
        gname: '客户姓名',
        gphone: '客户电话',
        vipcard: '会员卡号',
        recemoney: '消费现金',
        cardreduce: '卡耗消费',
        usebalance: '余额消费',
        usegiftbalance: '赠送余额消费',
        addtime: {
          value: '开单时间',
          fun: C.times
        },
        operatorname: '操作员',
        auditorname: '审核员'
      }
      let filter = C.copy(this.filter)
      filter.pageSize = 60000
      let data = await Order.getOrderList({ filter: JSON.stringify(filter) })
      let name
      if (filter.times && (filter.times[0] != null && filter.times[1] != null)) {
        name = C.time(Math.round(new Date(filter.times[0]).getTime() / 1000)) + '至' + C.time(Math.round(new Date(filter.times[1]).getTime() / 1000)) + '开单数据'
      } else {
        name = '历史所有开单数据'
      }
      await C.excel(name, header, data.data)
    },
    openGuestPage (id) {
      Guest.openGuestPage(id)
    }
  },
  mounted: function () {
    this.reupdate()
  },
  components: { Tags }
}
</script>

<style>
.order .el-table .cell {
	padding: 0px;
}
</style>